package com.petsdar.service;

import com.petsdar.dao.AccountMapper;
import com.petsdar.dao.KeeperAccountMapper;
import com.petsdar.pojo.Account;
import com.petsdar.pojo.KeeperAccount;
import com.petsdar.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/6/27
 * Time: 19:01
 * Description: 账户service层
 * 对登录操作做业务逻辑处理;
 * 对退出登录操作做业务逻辑处理；
 * 对密码修改操作做业务逻辑处理
 * Version: V1.0
 */
@Service
public class AccountServiceImpl implements AccountService{

    /** account类的dao层实例对象 */
    private AccountMapper accountMapper;

    /** keeperAccount类的dao层实例对象 */
    private KeeperAccountMapper keeperAccountMapper;

    /** power权限业务逻辑实现对象 */
    private PowerServiceImpl powerService = new PowerServiceImpl();

    @Autowired
    public void setAccountMapper(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    @Autowired
    public void setKeeperAccountMapper(KeeperAccountMapper keeperAccountMapper) {
        this.keeperAccountMapper = keeperAccountMapper;
    }


    /**
     * 通过前端发送的json数据，对其进行解析成对象，查看是否有正确的passID和用户名
     * 如果有则缓存中是否存在该passID以及检验用户名于passID是否一致，如一致说明该
     * 用户已登录，则返回成功信息。如果没有则更具用户名判断是否为admin用户或店长用
     * 户，从相应的数据库查询是否有该用户，成功则生成该用户的passID。以passID为key
     * 值，店铺名、(店铺，员工)权限、用户名为值存入缓存中，并将passID返回，响应到前端。
     * @param accountJson admin账户json数据
     * @return
     */
    @Override
    public HashMap<String,String> login(String accountJson) {
        Account account = null;
        KeeperAccount keeperAccount = null;
        HashMap<String, String> map = new HashMap<>();
        String passID;
        String userInfo;
        boolean status;

        account = JsonUtil.toObject(accountJson, Account.class, this.getClass());
        if (account!=null){
            if (account.getPassID() != null){
                status = JedisUtil.isKey(account.getPassID(),0);
                if (status){
                    String value = JedisUtil.getValue(account.getPassID(), 0);
                    Map valueMap = JsonUtil.toObject(value, Map.class, AccountServiceImpl.class);
                    if (valueMap.get("userName").equals(account.getUserName())){
                        map.put(Constant.PASS_ID, account.getPassID());
                        return map;
                    }
                }
            }

            if(account.getUserName().contains(Constant.KEEPER_POWER_ID)){
                keeperAccount = new KeeperAccount();
                keeperAccount.setKeeperAccountLoginName(account.getUserName());
                keeperAccount.setKeeperAccountLoginPwd(account.getPassword());
                keeperAccount.setKeeperAccountLoginPwd(MD5.getMD5(keeperAccount.getKeeperAccountLoginPwd(),keeperAccount.getKeeperAccountLoginName()));
                int loginResult = keeperAccountMapper.login(keeperAccount);
                String keeperShop = keeperAccountMapper.getKeeperShop(keeperAccount.getKeeperAccountLoginName());
                if (loginResult == 1){
                    passID = PassID.getPassID(keeperAccount.getKeeperAccountLoginName());
                    map.put("shop",keeperShop);
                    map.put("empPower",powerService.getPowerCacheString(Constant.PR_EMP_POWER));
                    map.put("chainPower",powerService.getPowerCacheString(Constant.PR_SHOP_POWER));
                    map.put("userName",keeperAccount.getKeeperAccountLoginName());
                    userInfo = JsonUtil.toJson(map, this.getClass());
                    map.clear();
                    status = JedisUtil.setexKey(passID, userInfo,0);
                    if (status){
                        map.put(Constant.PASS_ID, passID);
                        return map;
                    }
                }
            }else {
                account.setPassword(MD5.getMD5(account.getPassword(),account.getUserName()));
                int loginResult = accountMapper.login(account);
                if (loginResult == 1){
                    passID = PassID.getPassID(account.getUserName());
                    map.put("userName",account.getUserName());
                    map.put("shop",Constant.ADMIN_SHOP);
                    map.put("empPower",Constant.ADMIN_POWER);
                    map.put("chainPower",Constant.ADMIN_POWER);
                    userInfo = JsonUtil.toJson(map, this.getClass());
                    map.clear();
                    status = JedisUtil.setexKey(passID, userInfo,0);
                    if (status){
                        map.put(Constant.PASS_ID, passID);
                        return map;
                    }
                }
            }
        }
        return map;
    }

    /**
     * 对退出登录的请求，做出业务逻辑处理。
     * 将该用户的passID从缓存中删除，即可退出登录
     * 返回boolean值，true为移除成功，false表示移除失败或者缓存中没有该passID
     * @param outJson 用户退出登录的passID信息
     * @return
     */
    @Override
    public Boolean loginOut(String outJson) {
        HashMap<String,String> map = JsonUtil.toObject(outJson, HashMap.class, this.getClass());
        String passID = map.get("passID");
        if (JedisUtil.isKey(passID, 0)){
            return JedisUtil.removeKey(passID,0);
        }
        return false;
    }

    /**
     * admin账户更新密码，将新密码进行加密，再进行更新操作
     * @param account admin账户对象
     * @return
     */
    @Override
    public boolean updateAccount(Account account) {
        String md5 = MD5.getMD5(account.getPassword(), account.getUserName());
        account.setPassword(md5);
        int count = accountMapper.updateAccount(account);
        if (count == 1){
            return true;
        }
        return false;
    }

    /**
     * 重置密码方法。针对userJson数据，进行解析，对userName进行判断是店长账户还是
     * admin账户，根据不同的账户进行不同的操作，admin账户则直接调用本类的updateAccount
     * 方法。店长账户，首先需要将其实例化为keeperAccount对象，然后调用keeperAccountMapper
     * 对店长账户更新密码操作。返回boolean值，true为成功，false为失败
     * @param userJson 用户json数据
     * @return
     */
    @Override
    public boolean resetPwd(String userJson) {
        HashMap hashMap = JsonUtil.toObject(userJson, HashMap.class, AccountServiceImpl.class);
        String passID = (String) hashMap.get("passID");
        String userName = (String)hashMap.get("userName");
        String pwd = (String)hashMap.get("pwd");
        boolean iskey = JedisUtil.isKey(passID,0);
        if ( iskey && userName.contains(Constant.KEEPER_POWER_ID)){
            KeeperAccount keeperAccount = new KeeperAccount();
            keeperAccount.setKeeperAccountLoginName(userName);
            keeperAccount.setKeeperAccountLoginPwd(MD5.getMD5(pwd,userName));
            keeperAccount.setKtUpdateTime(new Date());
            int result = keeperAccountMapper.updateKeeperAccount(keeperAccount);
            if (result == 1){
                return true;
            }
        }else if (iskey){
            Account account = new Account();
            account.setUserName(userName);
            account.setPassword(pwd);
            account.setUpdateTime(new Date());
            boolean result = updateAccount(account);
            if (result){
                return true;
            }
        }
        return false;
    }
}
